package com.pinyougou.sellergoods.service.impl;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private TbBrandMapper brandMapper;
	
	@Override
	public List<TbBrand> findAll() {
		return brandMapper.selectByExample(null);
	}

	@Override
	public PageResult<TbBrand> findPage(int pageNo, int pageSize) {
		PageHelper.startPage(pageNo,pageSize);
		Page<TbBrand> page= (Page) brandMapper.selectByExample(null);
		return new PageResult<TbBrand>(page.getTotal(),page.getResult());
	}

	@Override
	public void add(TbBrand tbBrand) {
		brandMapper.insert(tbBrand);
	}

	@Override
	public TbBrand findOne(Long id) {
		return brandMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbBrand tbBrand) {
		brandMapper.updateByPrimaryKey(tbBrand);
	}

	@Override
	public void delete(Long[] ids) {

		for(Long id:ids){
			brandMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public PageResult<TbBrand> findPage(TbBrand tbBrand, int pageNo, int pageSize) {
		PageHelper.startPage(pageNo,pageSize);
		TbBrandExample tbBrandExample=new TbBrandExample();
		TbBrandExample.Criteria criteria= tbBrandExample.createCriteria();
		if (tbBrand!=null){
			if (StringUtils.isNotBlank(tbBrand.getName())){
				criteria.andNameLike("%"+tbBrand.getName()+"%");
			}
			if (StringUtils.isNotBlank(tbBrand.getFirstChar())){
				criteria.andFirstCharLike("%"+tbBrand.getFirstChar()+"%");
			}
		}
		Page<TbBrand> page= (Page) brandMapper.selectByExample(tbBrandExample);
		return new PageResult<TbBrand>(page.getTotal(),page.getResult());
	}

	@Override
	public List<Map> selectOptionList() {
		return brandMapper.selectOptionList();
	}
}
